﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.HU;
using HiStaff.Util;
using HiStaff.Dal;
using System.Windows.Forms;

namespace HiStaff.iProfile
{
    public partial class Commend : FormBase
    {
        public Commend()
        {
            InitializeComponent();
        }

        protected override bool FormLoad()
        {
            try
            {
                AddBasicCommandBar();
                SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
                BaseStatus = Util.Enum.FormStatus.View;
                SetStatusInputControl(this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_COMMEND>("APRS_HU_COMMEND", new EMPLOYEE_SEARCH { EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID")) });
                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void BindDataByEmp()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_EMPLOYEE>("PRS_HU_EMPLOYEE", new EMPLOYEE_SEARCH { ORGID = trvOrg.OrgId, TERMINATE = hiCheckBox1.Value });
                grdEmpList.DataSource = lstEmployee;
                grdEmpList.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void trvOrg_AfterSelect_1(object sender, TreeViewEventArgs e)
        {
            try
            {
                BindDataByEmp();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdEmpList_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (grdEmpList.GetValue("ID") == null)
                {
                    grdMain.DataSource = null;
                    grdMain.Refresh();
                    ResetInputControl(this);
                }
                else
                {
                    BindData();
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdMain_SelectionChanged_1(object sender, EventArgs e)
        {
            BindDataFromGridToForm(grdMain, this);
        }

        private void hiCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            BindDataByEmp();
        }

        protected override bool SaveButtonClick()
        {
            try
            {
                var objSave = new HU_COMMEND();
                ValueControlsToObject(objSave, this);
                if (BaseStatus == Util.Enum.FormStatus.Add)
                {
                    objSave.ID = 0;
                }
                OracleHelper.ExcuteNonQuery("APRU_HU_COMMEND", objSave);

                //COMMEND EMPLOYEE                
                objSave.COMMENDID = objSave.ID;
                objSave.EMPLOYEEID = hiEmployeeSelection1.EmployeeSelected[0].ID;
                OracleHelper.ExcuteNonQuery("APRD_HU_COMMENDEMPLOYEE", objSave);
                foreach (var emp in hiEmployeeSelection1.EmployeeSelected)
                {
                    objSave.EMPLOYEEID = emp.ID;
                    OracleHelper.ExcuteNonQuery("APRU_HU_COMMENDEMPLOYEE", objSave);
                }

                BindData();
                MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool CancelButtonClick()
        {
            try
            {
                BindDataFromGridToForm(grdMain, this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool DeleteButtonClick()
        {
            var objDel = new HU_COMMEND();
            ValueControlsToObject(objDel, this);
            objDel.COMMENDID = objDel.ID;
            if (OracleHelper.ExcuteNonQuery("APRD_HU_COMMEND", objDel) && OracleHelper.ExcuteNonQuery("APRD_HU_COMMENDEMPLOYEE", objDel))
            {
                MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                BindData();
                return true;
            }
            return false;
        }

        private void hiCheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (hiCheckBox2.Checked)
                {
                    hiEmployeeSelection1.SelectMultiEmployee = true;
                }
                else
                {
                    hiEmployeeSelection1.SelectMultiEmployee = false;
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }

        }



    }
}
